<?php
class Engine_Utility_String 
{
	public static function latin1_to_ascii($string){
		$string = preg_replace("/[ạáàảãâậấầẩẫăặắằẳẫäæå]/u","a",$string);
		$string = preg_replace("/[ẠÁÀẢÃÂẬẤẦẨẪĂẮẰẲẴẶÄÆÅ]/u","A",$string);
		$string = preg_replace("/[þ]/u","b",$string);
		$string = preg_replace("/[Þ]/u","B",$string);
		$string = preg_replace("/[ç]/u","c",$string);
		$string = preg_replace("/[Ç]/u","C",$string);
		$string = preg_replace("/[đð]/u","d",$string);
		$string = preg_replace("/[Đ]/u","D",$string);
		$string = preg_replace("/[ẹéèẻẽêếềểễệë]/u","e",$string);
		$string = preg_replace("/[ẸÉÈẺẼÊẾỀỂỄỆË]/u","E",$string);
		$string = preg_replace("/[ịíìỉĩîï]/u","i",$string);
		$string = preg_replace("/[ỊÍÌỈĨÎÏ]/u","I",$string);
		$string = preg_replace("/[ñ]/u","n",$string);
		$string = preg_replace("/[Ñ]/u","N",$string);
		$string = preg_replace("/[óòỏõọôốồổỗộơớờởỡợöøº]/u","o",$string);
		$string = preg_replace("/[ÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÖØ]/u","O",$string);
		$string = preg_replace("/[úùủũụưứừửữựûü]/u","u",$string);
		$string = preg_replace("/[ÚÙỦŨỤƯỨỪỬỮỰÛÜ]/u","U",$string);
		$string = preg_replace("/[ŕ]/u","r",$string);
		$string = preg_replace("/[Ŕ]/u","R",$string);
		$string = preg_replace("/[ýỳỷỹỵ]/u","y",$string);
		$string = preg_replace("/[ÝỲỶỸỴ]/u","Y",$string);
		$string = preg_replace("/[ž]/u","z",$string);
		return $string = preg_replace("/[Ž]/u","Z",$string);
	}
	
	public static function uri_encode($string, $length=0){
		$text = preg_replace('/(-{2,})|(\.{2,})+/', '-', strtolower(trim(preg_replace('/[^\w.]|[|_]+/', '-', self::latin1_to_ascii($string)), '-')));
		if(is_int($length) && $length > 0 && mb_strlen($text, "UTF-8") > $length) 
		{
			$text = mb_substr($text, 0, $length, "UTF-8");
		}
		return $text;
		//Read more: http://msdn.microsoft.com/en-us/library/az24scfc.aspx
		//return $text = strtolower(trim(preg_replace('/[\W|_]+/','-',self::latin1_to_ascii($string)),'-'));
	}
	
	static function _latin1_to_ascii($str) {
		$chars = array(
			'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','ắ','ằ','ẳ','ẵ','ặ','á','à','ả','ã','ạ','â','ă','ä','å'),
			'A' =>  array('Ấ','Ầ','Ẩ','Ẫ','Ậ','Ắ','Ằ','Ẳ','Ẵ','Ặ','Á','À','Ả','Ã','Ạ','Â','Ă','Ä','Å'),
			'c' =>  array('ç'),
			'C' =>  array('Ç'),
			'e' =>	array('ế','ề','ể','ễ','ệ','é','è','ẻ','ẽ','ẹ','ê','ë'),
			'E' =>  array('Ế','Ề','Ể','Ễ','Ệ','É','È','Ẻ','Ẽ','Ẹ','Ê','Ë'),
			'i'	=>	array('í','ì','ỉ','ĩ','ị','ï','î'),
			'I' =>  array('Í','Ì','Ỉ','Ĩ','Ị','Ï','Î'),
			'n' =>  array('ñ'), 
			'N' =>  array('Ñ'),
			'o'	=>	array('ố','ồ','ổ','ỗ','ộ','ớ','ờ','ở','ỡ','ợ','ó','ò','ỏ','õ','ọ','ô','ơ','ø','ö'),
			'O' =>  array('Ố','Ồ','Ổ','Ô','Ộ','Ớ','Ờ','Ở','Ỡ','Ợ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ','Ø','Ö'),
			's' =>  array('š'),
			'S' =>  array('Š'),
			'u'	=>	array('ứ','ừ','ử','ữ','ự','ú','ù','ủ','ũ','ụ','ư','û','ü','û'),
			'U' =>  array('Ứ','Ừ','Ử','Ữ','Ự','Ú','Ù','Ủ','Ũ','Ụ','Ư','Û','Ü','Û'),
			'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','ÿ'),
			'Y' =>  array('Ý','Ỳ','Ỷ','Ỹ','Ỵ','Ÿ'),
			'd'	=>	array('đ'),
			'D' =>  array('Đ'),
		);
		foreach ($chars as $key => $arr) 
			foreach ($arr as $val)
				$str = str_replace($val,$key,$str);
		return $str;
	}
	
	static function utf8_to_ncrdecimal($str) {
		return preg_replace("/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e", '_utf8_to_ncrdecimal("\\1")', $str);
	}
	
	private static function _utf8_to_ncrdecimal($str) {
		$ret = 0;
		foreach( (str_split(strrev(chr((ord($str{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($str, 1)))) as $k => $v)
			$ret += (ord($v) % 128) * pow(64, $k);
		return "&#$ret;";
	} 
	
	static function nrcdecimal_to_utf8($str) {
		return html_entity_decode($str,ENT_NOQUOTES,'UTF-8');
	}
	
	static function split_str($str, $delimiter=' ') {
		if(!empty($str) && strlen($delimiter)==1){
			$str_arr=array();
			$str_length=strlen($str);
			$first=0; $last=0;
			for($i=0; $i<$str_length; $i++)	{
				if($str[$i]!=$delimiter){
					$word=''; 
					$first=$last=$i;
					while($str[$last]!=$delimiter) {
						$word .=$str[$last];
						$last=$last+1;
						if($last>=$str_length)
							break;
					}
					array_push($str_arr,$word);
					$i=$last;
				}
			}
			return $str_arr;
		}
		return FALSE;
	}
	
	static function truncate($str, $length, $puncs=".!?:;,-", $etc="...") {
		if (strlen($str) > $length && is_int($length)) {
			if($length<0) $length=abs($length);
			$str=substr($str,0,$length);
			$pos = strrpos($str, " ");
			if(is_bool($pos)&&!$pos)
				$str= substr($str,0,$length);
			else
				$str= substr($str, 0, $pos);
			//clear punctuations
			if(strspn(strrev($str),  $puncs)!=0)
				$str=substr($str, 0, -strspn(strrev($str),  $puncs));
			$str .= $etc;
		}
		return $str;
	}
}
